package com.camel.chart.utils;

import com.camel.chart.call.DialogOkAfterCall;
import com.camel.chart.call.DialogYesNoYesCall;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.scene.control.*;

public class DialogUtil {
    public static void alert(String title, String hederText, String contentText, DialogOkAfterCall okAfterCall) {
        Platform.runLater(() -> {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle(title);
            alert.setHeaderText(hederText);
            alert.setContentText(contentText);
            Button btnOk = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
            btnOk.setOnAction(event -> okAfterCall.call());
            alert.show();
        });
    }

    public static void yesNo(String title, String hederText, String contentText, DialogYesNoYesCall okAfterCall, DialogYesNoYesCall noCall) {
        Platform.runLater(() -> {
            Dialog<ButtonType> dialog = new Dialog<>();
            dialog.setTitle(title);
            dialog.setHeaderText(hederText);
            dialog.setContentText(contentText);

            DialogPane dialogPane = dialog.getDialogPane();
            dialogPane.setPrefSize(500, 300);

            ObservableList<ButtonType> buttonTypes = dialogPane.getButtonTypes();
            buttonTypes.addAll(ButtonType.OK, ButtonType.CANCEL);
            Button btnOk = (Button) dialogPane.lookupButton(ButtonType.OK);
            Button btnCancel = (Button) dialogPane.lookupButton(ButtonType.CANCEL);

            btnOk.setOnAction(event -> okAfterCall.call());
            btnCancel.setOnAction(event -> noCall.call());

            dialog.setOnCloseRequest(event -> noCall.call());
            dialog.showAndWait().filter(buttonType -> buttonType == ButtonType.OK).ifPresent(buttonType -> System.out.println("doAction"));
        });
    }

    public static void choice(ObservableList<String> itemList) {
        ChoiceDialog<String> choiceDialog = new ChoiceDialog<>();
        choiceDialog.setSelectedItem("item 3");
        choiceDialog.getItems().addAll(itemList);

        Button btnOk = (Button) choiceDialog.getDialogPane().lookupButton(ButtonType.OK);
        btnOk.setOnAction(event -> System.out.println("Ok"));
        choiceDialog.selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        });
    }
}
